package com.springcloud.aop;



import com.springcloud.dto.SupTaskDto;
import com.springcloud.model.SysLogModel;
import com.springcloud.service.LogService;
import com.springcloud.utils.RequestIpUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.*;


@Aspect
@Configuration
public class LogAspect {

    private LogService logService;
    @Autowired
    public void setLogService(LogService logService) {
        this.logService = logService;
    }

    /**
     * 用户创建订单
     */
    @Pointcut("@annotation(OperationLogger)")
    public void controllerAspect() {
        System.out.println("切入点》》》》");
    }
   // @Around("controllerAspect()")
    public Object doAround(ProceedingJoinPoint joinPoint){
        System.out.println("环绕通知");
        handleLog(joinPoint);
        return null;
    }

    @AfterReturning("controllerAspect()")
    public void doAfterReturn(JoinPoint joinPoint) {
        System.out.println("==========后置通知=========");
        handleLog(joinPoint);
    }

    private void handleLog(JoinPoint joinPoint) {
        try {
            HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
            System.out.println(request.toString());
            //UserInfo user = (UserInfo) request.getSession().getAttribute("userInfo");
            //获取横切的方法名
            String methodName = joinPoint.getSignature().getName();
//            String methodName = signature.substring(
//                    signature.lastIndexOf(".")+1,signature.indexOf("("));
            //获取拦截的class
            String classType = joinPoint.getTarget().getClass().getName();
            //加载这个类
            Class<?> clazz = Class.forName(classType);
            //获取这个类上的方法名
            Method[] methods = clazz.getDeclaredMethods();
            Object[] args = joinPoint.getArgs();
            System.out.println("methodName:" + methodName);
            // 循环判断，对所有方法进行循环遍历
            for (Method method : methods) {
                // 如果这个方法上面的注解是否含有自定义的注解
                // 并且方法名等于切点访问的方法名
                if (!(method.isAnnotationPresent(OperationLogger.class)
                        && method.getName().equals(methodName)))continue;
                List<Object> list=new ArrayList<>(Collections.singletonList(args[0]));
                if(methodName.equals("insertSingleTask")||methodName.equals("insertWMSTask")){
                    for(Object o:list){
                        SupTaskDto dto=(SupTaskDto) o;
                        //获取执行的任务号
                        String taskno=dto.getTaskNo();
                        //获取method的注解
                        OperationLogger operationLogger = method.getAnnotation(OperationLogger.class);
                        SysLogModel sysLog = new SysLogModel();
                        sysLog.setUser_name("未登录用户");
                        sysLog.setOptIp(RequestIpUtil.getRemoteHost(request));
                        sysLog.setCreate_time(new Date());
                        sysLog.setOperation(operationLogger.option());
                        sysLog.setContent(operationLogger.modelName());
                        logService.save(sysLog);
                    }
                }else{
                    List<Object> arrayList=(ArrayList) list.get(0);
                    for(Object o:arrayList){
                        SupTaskDto dto=(SupTaskDto) o;
                        String taskno=dto.getTaskNo();
                        //获取method的注解
                        OperationLogger operationLogger = method.getAnnotation(OperationLogger.class);
                        SysLogModel sysLog = new SysLogModel();
                        sysLog.setUser_name("未登录用户");
                        sysLog.setOptIp(RequestIpUtil.getRemoteHost(request));
                        sysLog.setCreate_time(new Date());
                        sysLog.setOperation(operationLogger.option());
                        sysLog.setContent(operationLogger.modelName());
                        logService.save(sysLog);
                    }

                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}